package dal

import (
	"fmt"

	. "7qule.com/sd3/CrossServer/src/bll/kuangzhan/model"
	baseDal "7qule.com/sd3/CrossServer/src/dal"
)

// 镖局信息访问结构
type globalKuangzhanOccupyDal struct{}

// 访问结构对象
var GlobalKuangzhanOccupyDal = new(globalKuangzhanOccupyDal)

// 初始化
func init() {
	// 注册到同步对象中
	baseDal.GetSyncMgr().RegisterSyncObj(GlobalKuangzhanOccupyDal.TableName())
}

// TableName 表名
func (this *globalKuangzhanOccupyDal) TableName() string {
	return "g_kuangzhan_occupy"
}

//
// GetData
// @Description: 从数据库获取玩家占领数据
// @receiver this
// @return []*GlobalKuangzhanOccupy:获取到的数据
// @return error:错误信息
//
func (this *globalKuangzhanOccupyDal) GetData() ([]*GlobalKuangzhanOccupy, error) {
	sql := "SELECT `PlayerId`,`OccupyTime` FROM `g_kuangzhan_occupy`;"

	db := baseDal.GetGameDB().DB()
	execResult, err := db.Query(sql)
	if err != nil {
		baseDal.WriteLog("GlobalKuangzhanOccupyDal", err)

		return nil, err
	}

	defer execResult.Close()

	// 读取数据
	var result = make([]*GlobalKuangzhanOccupy, 0, 16)
	for execResult.Next() {
		var tempNewItem = NewGlobalKuangzhanOccupy()
		err = execResult.Scan(
			&tempNewItem.PlayerId,
			&tempNewItem.OccupyTime)

		if err != nil {
			baseDal.WriteLog("GlobalKuangzhanOccupyDal", err)
			return nil, err
		}

		result = append(result, tempNewItem)
	}

	return result, nil
}

//
// Insert
// @Description:插入数据
// @receiver this
// @param data:操作的数据项
//
func (this *globalKuangzhanOccupyDal) Insert(data *GlobalKuangzhanOccupy) {
	sql := fmt.Sprintf("INSERT INTO `g_kuangzhan_occupy` (`PlayerId`,`OccupyTime`) VALUES ('%v', '%v') ;",
		data.PlayerId,
		data.OccupyTime)

	baseDal.GetGameDB().Exec(sql)
}

//
// Update
// @Description:更新数据
// @receiver this
// @param data:操作的数据项
//
func (this *globalKuangzhanOccupyDal) Update(data *GlobalKuangzhanOccupy) {
	sql := fmt.Sprintf("UPDATE `g_kuangzhan_occupy` SET `OccupyTime`='%v' WHERE `PlayerId`='%v';",
		data.OccupyTime,
		data.PlayerId)
	baseDal.GetGameDB().Exec(sql)
}

//
// Delete
// @Description:删除数据
// @receiver this
// @param data:操作的数据项
//
func (this *globalKuangzhanOccupyDal) Delete(data *GlobalKuangzhanOccupy) {
	sql := fmt.Sprintf("DELETE FROM g_kuangzhan_occupy where `PlayerId`='%v';", data.PlayerId)

	baseDal.GetGameDB().Exec(sql)
}
